package com.mlz.mlzmall.ui.activity.act.personal.myActivityPack;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyActionBean;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyActivityRequestUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyCenterUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.activity.act.personal.ReceiveAddress;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @description 抽奖活动
 * Created by 安帅 on 2016/5/27 21:31.
 * PackageName com.mlz.mlzmall.ui.activity.act.personal.myActivityPack.
 * ProjectName mlzmall_android.
 */
public class ActionActivity extends VolleyBaseActivity implements View.OnClickListener {
    private static final String TGA = "ActionActivity";
    public static String MY_ACTIVITY_IMAGEURL = "MY_ACTIVITY_IMAGEURL";//图片
    public static String MY_ACTIVITY_TYPE = "MY_ACTIVITY_TYPE";//类型
    public static String MY_ACTIVITY_PROMOTIONID = "MY_ACTIVITY_PROMOTIONID";//id
    public static String MY_ACTIVITY_STATE = "MY_ACTIVITY_STATE";//状态
    private int type;//活动类型
    int promotionId;
    int state2;
    int state;
    String imageUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action_duihuan);
        assignViews();
        setViewContent();
        setViewClickListener();
        initData();
    }

    private TopTitleView actionTopTitle;
    private RelativeLayout lyDizhi; //个人信息
    private TextView tvName;//名字
    private TextView tvPhone;//电话
    private TextView tvAddress;//地址
    private RelativeLayout lyMoren;
    private LinearLayout imEnter;
    private ImageView imProduct;
    private Button lyDuihuan;//赠品兑换
    private Button lyDuihuanOver;//已兑换
    private Button lyDuihuanFinish;//活动已结束

    /**
     * 初始化控件
     */
    private void assignViews() {
        actionTopTitle = (TopTitleView) findViewById(R.id.action_top_title);
        lyDizhi = (RelativeLayout) findViewById(R.id.ly_dizhi);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        lyMoren = (RelativeLayout) findViewById(R.id.ly_moren);
        imEnter = (LinearLayout) findViewById(R.id.im_enter);
        imProduct = (ImageView) findViewById(R.id.im_product);
        lyDuihuan = (Button) findViewById(R.id.ly_duihuan);
        lyDuihuanOver = (Button) findViewById(R.id.ly_duihuan_over);
        lyDuihuanFinish = (Button) findViewById(R.id.ly_duihuan_finish);
    }

    /**
     * 设置内容
     */
    private void setViewContent() {
        actionTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        actionTopTitle.setTitleButtonContent("赠品兑换", null, null);

    }

    /**
     * 事件监听
     */
    private void setViewClickListener() {
        actionTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {

            @Override
            public void onLeftBtnClick(View button) {
                finish();
            }
        });
        lyDuihuan.setOnClickListener(this);
        lyDuihuanOver.setOnClickListener(this);
        lyDuihuanFinish.setOnClickListener(this);
        imEnter.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setViewInitData();
    }

    /**
     * 接收数据
     */
    private void initData() {
        String stringExtra = getIntent().getStringExtra(MY_ACTIVITY_PROMOTIONID);
        imageUrl = getIntent().getStringExtra(MY_ACTIVITY_IMAGEURL);
        type = Integer.parseInt(getIntent().getStringExtra(MY_ACTIVITY_TYPE));
        promotionId = Integer.parseInt(stringExtra);
        state2 = getIntent().getIntExtra("state2", -1);
        state = getIntent().getIntExtra(MY_ACTIVITY_STATE, -1);
        if (state2 == -1) {
            executeRequest(MyActivityRequestUtils.getMLZAcceptTheView(mContext, promotionId, type, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    try {
                        JSONObject json = new JSONObject(s);
                        int status = json.getInt("status");
                        if (0 == status) {
                            String data = json.getString("data");
                            JSONObject json1 = new JSONObject(data);
                            int addressId = json1.getInt("addressId");
                            if (addressId != -1) {
                                setViewInitData();
                                imEnter.setEnabled(false);
                                lyDizhi.setVisibility(View.VISIBLE);
                                lyMoren.setVisibility(View.GONE);
                            } else {
                                lyDizhi.setVisibility(View.GONE);
                                lyMoren.setVisibility(View.VISIBLE);
                                imEnter.setEnabled(true);

                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }));
        } else {
            tvName.setText(fullname);
            tvPhone.setText(telephone);
            tvAddress.setText(address);
        }
    }

    public static final int ACTIVITY_REQUESTCODE = 0x001;//领奖地址回调
    public static final String ACTIVITY_STATUS = "1001";//领奖地址状态码
    String fullname;//收货人
    String telephone;//电话
    String address;//地址
    String addressId;//收货地址id

    /**
     * 请求数据
     */
    private void setViewInitData() {
        executeRequest(MyActivityRequestUtils.getMLZSweepStakes(mContext, promotionId, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.i(TGA, "我的抽奖活动的数据：" + jsonObject.toString());
                Gson gosn = new Gson();
                MyActionBean myActionBean = gosn.fromJson(jsonObject.toString(), MyActionBean.class);
                int status = myActionBean.getStatus();
                if (0 == status) {
                    String status1 = myActionBean.getData().getStatus();
                    if (status1.equals("UNJOINED")) {//可兑换
                        lyDuihuan.setVisibility(View.VISIBLE);
                    } else if (status1.equals("JOINED")) {//已兑换
                        if (myActionBean.getData().isGiftReceive() == true) {
                            lyDuihuanOver.setVisibility(View.VISIBLE);
                        } else if (state2 == -1) {
                            switch (state) {
                                case 1:
                                    lyDuihuanOver.setVisibility(View.VISIBLE);
                                    lyDuihuanOver.setText("已兑换");
                                    imEnter.setFocusable(false);
                                    break;
                                case 4:
                                case 3:
                                case 2:
                                    lyDuihuan.setVisibility(View.VISIBLE);//可兑换
                                    lyDuihuan.setText("兑换赠品");
                                    break;
                                case 5:
                                    lyDuihuanOver.setVisibility(View.VISIBLE);
                                    lyDuihuanOver.setText("已兑换");
                                    imEnter.setFocusable(false);
                                    break;

                                default:
                                    lyDuihuan.setVisibility(View.VISIBLE);//可兑换
                                    lyDuihuan.setText("兑换赠品");
                                    break;
                            }
                        } else {
                            switch (state) {

                                case 5:
                                    lyDuihuan.setVisibility(View.VISIBLE);//可兑换
                                    lyDuihuan.setText("兑换赠品");
                                    break;
                                case 6:
                                    lyDuihuanOver.setVisibility(View.VISIBLE);
                                    lyDuihuanOver.setText("已兑换");
                                    imEnter.setFocusable(false);
                                    break;

                                default:
                                    lyDuihuan.setVisibility(View.VISIBLE);//可兑换
                                    lyDuihuan.setText("兑换赠品");
                                    break;
                            }
                        }


                    } else if (status1.equals("EXPIRED")) {//已过期
                        lyDuihuanFinish.setVisibility(View.VISIBLE);
                    }
                    if (myActionBean.getData().equals(null)) {
                        MyCenterUtils.ToastText("数据不存在");
                    } else {
                        fullname = (String) myActionBean.getData().getShippingFullname();
                        telephone = (String) myActionBean.getData().getShippingTelephone();
                        address = (String) myActionBean.getData().getShippingAddress();
                        tvName.setText(fullname);
                        tvPhone.setText(telephone);
                        tvAddress.setText(address);


                    }

                    ImageLoader.getInstance().displayImage(imageUrl, imProduct);

                }

            }
        }));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ACTIVITY_REQUESTCODE && resultCode == ReceiveAddress.RESULT_OK) {
            if (data != null) {
                Bundle bundle = data.getExtras();
                fullname = bundle.getString("FullName");
                telephone = bundle.getString("Telephone");
                address = bundle.getString("Address");
                addressId = bundle.getString("AddressId");
                LogUtils.d("信息回调："+fullname + telephone + address);
                //设置信息
                lyMoren.setVisibility(View.GONE);
                lyDizhi.setVisibility(View.VISIBLE);
                tvName.setText(fullname);
                tvPhone.setText(telephone);
                tvAddress.setText(address);
            }
        }
    }

    private static final String TAG_DIALOG_ADDRESS = "TAG_DIALOG_ADDRESS";//
    private static final String TAG_DIALOG_LOGIN = "TAG_DIALOG_LOGIN";
    private static final String TAG_DIALOG_DISTRIBUTION = "TAG_DIALOG_DISTRIBUTION";
    private Dialog mDialog;

    /**
     * 创建对话框
     */
    private void createDialog(String tag) {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        mDialog = null;
        switch (tag) {
            case TAG_DIALOG_ADDRESS:
                if (mDialog == null) {
                    mDialog = new Dialog(mContext);
                    View view = LayoutInflater.from(mContext).inflate(R.layout.dialogview_duihuan_address, null);
                    mDialog.setContentView(view);
                    view.findViewById(R.id.continue_shopping).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mDialog.dismiss();
                        }
                    });
                }
                break;
            case TAG_DIALOG_LOGIN:
                View view1 = LayoutInflater.from(mContext).inflate(R.layout.dialogview_duihuan_address, null);
                mDialog.setContentView(view1);
                TextView body = (TextView) view1.findViewById(R.id.dialog_body);
                body.setText("需要登录");

                view1.findViewById(R.id.continue_shopping).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });
                break;
            case TAG_DIALOG_DISTRIBUTION:
                View view2 = LayoutInflater.from(mContext).inflate(R.layout.dialog_duihuan, null);
                mDialog.setContentView(view2);
                TextView name_text = (TextView) view2.findViewById(R.id.tv_duihuan_name);
                TextView phone_text = (TextView) view2.findViewById(R.id.tv_duihuan_phone);
                TextView address_text = (TextView) view2.findViewById(R.id.tv_duihuan_address);
                name_text.setText("收货人:" + fullname);
                phone_text.setText("收货电话:" + telephone);
                address_text.setText(address);
                view2.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                        executeRequest(MyActivityRequestUtils.getMLZToReceivePrizeAction(mContext, type, Integer.parseInt(addressId), promotionId, new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                Log.i(TGA, "领取赠品返回的数据：" + jsonObject.toString());
                                try {
                                    JSONObject json = new JSONObject(jsonObject.toString());
                                    int status = json.getInt("status");
                                    String msg = json.getString("msg");
                                    if (0 == status) {
                                        MyCenterUtils.ToastText(msg);
                                    } else {
                                        MyCenterUtils.ToastText(msg);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }));
                    }
                });
                view2.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });
                break;
        }
        if (mDialog != null) mDialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ly_duihuan:
                if (tvName.getText().toString().equals("") && tvPhone.getText().toString().equals("")) {
                    createDialog(TAG_DIALOG_ADDRESS);
                } else if (MyApplication.getInstance().loginUser.getAccout() == null) {
                    createDialog(TAG_DIALOG_LOGIN);
                } else {
                    createDialog(TAG_DIALOG_DISTRIBUTION);
                }
                break;
            case R.id.im_enter:
                Intent intent = new Intent(ActionActivity.this, ReceiveAddress.class);
                intent.putExtra("activity_status", ACTIVITY_STATUS);
                startActivityForResult(intent, ACTIVITY_REQUESTCODE);
                break;
        }
    }
}
